package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.ISysUpdateLogService;
import com.ruoyi.system.vo.SysUpdateLogVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 更新日志Controller
 *
 * @author tmac-1
 * @date 2023-05-12
 */
@Api(tags = "更新日志Controller")
@RestController
@RequestMapping("/system/updateLog/")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class SysUpdateLogController extends BaseController {

    private final ISysUpdateLogService sysUpdateLogService;

    @ApiOperation("分页查询更新日志列表")
    @PreAuthorize("@ss.hasPermi('system:updateLog:list')")
    @GetMapping("queryUpdateLogListByPage")
    public TableDataInfo<SysUpdateLogVo> queryUpdateLogListByPage(SysUpdateLogVo sysUpdateLogVo) {
        startPage();
        List<SysUpdateLogVo> updateLogList = sysUpdateLogService.queryUpdateLogList(sysUpdateLogVo);
        return getDataTable(updateLogList);
    }

    @ApiOperation("查询更新日志所有列表")
    @GetMapping("queryUpdateLogList")
    public AjaxResult queryUpdateLogList(SysUpdateLogVo sysUpdateLogVo) {
        return AjaxResult.success("查询成功", sysUpdateLogService.queryUpdateLogList(sysUpdateLogVo));
    }

    @ApiOperation("导出更新日志列表")
    @PreAuthorize("@ss.hasPermi('system:updateLog:export')")
    @Log(title = "导出更新日志列表", businessType = BusinessType.EXPORT)
    @PostMapping("exportUpdateLogList")
    public void exportUpdateLogList(HttpServletResponse response, SysUpdateLogVo sysUpdateLogVo) {
        List<SysUpdateLogVo> updateLogList = sysUpdateLogService.queryUpdateLogList(sysUpdateLogVo);
        ExcelUtil<SysUpdateLogVo> util = new ExcelUtil<>(SysUpdateLogVo.class);
        util.exportExcel(response, updateLogList, "更新日志数据");
    }

    @ApiOperation("查询更新日志对象信息")
    @PreAuthorize("@ss.hasPermi('system:updateLog:query')")
    @GetMapping("queryUpdateLogById/{id}")
    public AjaxResult queryUpdateLogById(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", sysUpdateLogService.queryUpdateLogById(id));
    }

    @ApiOperation("新增更新日志")
    @PreAuthorize("@ss.hasPermi('system:updateLog:add')")
    @Log(title = "新增更新日志", businessType = BusinessType.INSERT)
    @PostMapping("addUpdateLog")
    public AjaxResult addUpdateLog(@RequestBody SysUpdateLogVo sysUpdateLogVo) {
        return toAjax(sysUpdateLogService.save(sysUpdateLogVo));
    }

    @ApiOperation("修改更新日志")
    @PreAuthorize("@ss.hasPermi('system:updateLog:edit')")
    @Log(title = "修改更新日志", businessType = BusinessType.UPDATE)
    @PostMapping("updateUpdateLogById")
    public AjaxResult updateUpdateLogById(@RequestBody SysUpdateLogVo sysUpdateLogVo) {
        return toAjax(sysUpdateLogService.updateById(sysUpdateLogVo));
    }

    @ApiOperation("删除更新日志")
    @PreAuthorize("@ss.hasPermi('system:updateLog:remove')")
    @Log(title = "删除更新日志", businessType = BusinessType.DELETE)
    @GetMapping("removeUpdateLog/{ids}")
    public AjaxResult removeUpdateLog(@PathVariable Long[] ids) {
        return toAjax(sysUpdateLogService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
